﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CANO.Data.DAOs.Usuario;
using CANO.Data.DAOs;

namespace CANO.WebSite
{
	public partial class Login : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			btnIniciarSesion.Click += new EventHandler(btnIniciarSesion_Click);

			if (!Page.IsPostBack)
			{
				if (Request.QueryString["Logout"] != null)
					Session.RemoveAll();
			}
		}

		void btnIniciarSesion_Click(object sender, EventArgs e)
		{
			try 
			{	        
				if(UserHelper.ValidateUser(txtUsuario.Value, txtPassword.Value))
				{
					Session["CurrentUserID"] = new UsuarioDAO().GetUserIDByUser(txtUsuario.Value);
					Session["CurrentRolID"] = new UsuarioDAO().getRolByUser(txtUsuario.Value);
                    Session.Timeout = 1000;

					Response.Redirect("Index.aspx");
				}
				else
				{
				lblErrorMessage.Text = "Usuario o Password incorrecto.";
				pnlError.Visible = true;
				}
			}
			catch (Exception)
			{
				lblErrorMessage.Text = "Usuario o Password incorrecto.";
				pnlError.Visible = true;
			}
			
			
		}
	}
}